package com.hsurosy.hsuchain.nft.datasource.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.hsurosy.hsuchain.nft.datasource.handler.DataObjectHandler;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 数据源配置类
 *
 * @Author Hsu琛君珩
 * @Date 2024-10-04 19:12
 * @Description
 * @Version: v1.0.0
 */
@Configuration
@MapperScan(basePackages = "com.hsurosy.hsuchain.nft.*.infrastructure.mapper")
public class DatasourceConfiguration {

    /**
     * 自定义元数据处理器
     *
     * @return DataObjectHandler 实例，用于处理数据对象的元数据
     */
    @Bean
    public DataObjectHandler myMetaObjectHandler() {
        return new DataObjectHandler();
    }

    /**
     * 配置 MyBatis Plus 拦截器
     *
     * @return MybatisPlusInterceptor 实例，包含分页、乐观锁等插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 乐观锁插件，防止数据并发更新时产生冲突
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        // 防止全表更新和删除的安全插件，避免误操作
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        // 分页插件，支持数据库分页查询
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}
